package com.ldx.music.web;

import com.auth0.jwt.JWT;
import com.ldx.music.info.ImageInfo;
import com.ldx.music.info.MusicInfo;
import com.ldx.music.info.UserInfo;
import com.ldx.music.result.ResponseResult;
import com.ldx.music.util.JWTUtils;
import org.apache.catalina.User;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@RestController
@RequestMapping("/music")
public class MusicController {

    @RequestMapping("/login")
    public ResponseResult<UserInfo> login(@RequestBody UserInfo userInfo){

        System.out.println(userInfo);
        if("jack".equalsIgnoreCase(userInfo.getUserName()) && "123".equalsIgnoreCase(userInfo.getUserPass())){

            System.out.println("成功");

            //构建token
            Map<String,Object> map = new HashMap<>();
            map.put("name","jack");
            final String token = JWTUtils.sign(map);

            return ResponseResult.SUCCESS(new UserInfo(10001,"jack","","head.jpg",token));
        }

        return ResponseResult.FAIL("认证失败");
    }

    @RequestMapping("/test")
    public ResponseResult<String> testToken(HttpServletRequest request){
        final String token = request.getHeader("Authorization");
        System.out.println("开始校验" + token);
        final boolean verify = JWTUtils.verify(token);
        if (verify){
            return ResponseResult.SUCCESS("token有效,可以正常访问系统");
        }

        return ResponseResult.FAIL("token异常");
    }

    /*
            MusicInfo musicInfo1 = new MusicInfo(1313102149,"泡沫","http://p1.music.126.net/3XEICRFzQPrHPUMD0xm1Jw==/109951163069323980.jpg?param=130y130",new SongInfo("G.E.M.邓紫棋","我就是泡沫","专辑xxx"));
        MusicInfo musicInfo2 = new MusicInfo(2097278594,"顶一万句","http://p2.music.126.net/1x5wEW75P9OmNmd5zXVTvQ==/109951169036783876.jpg?param=130y130",new SongInfo("何家乐","第一镜 II","第一镜 II"));
        MusicInfo musicInfo3 = new MusicInfo(115162,"月半小夜曲","http://p1.music.126.net/SIFuIDfMNbuY9-IQcbTz5w==/109951166890517973.jpg?param=130y130",new SongInfo("李克勤","经典金曲","专辑xxx"));
        MusicInfo musicInfo4 = new MusicInfo(202369,"画","http://p1.music.126.net/wldFtES1Cjnbqr5bjlqQbg==/18876415625841069.jpg?param=130y130",new SongInfo("赵雷","赵小雷","赵小雷赵小雷赵小雷"));
        MusicInfo musicInfo5 = new MusicInfo(32717112,"Lean On (CRNKN Remix)","http://p1.music.126.net/8Lw4RZfn0-_QlmTTr4BuLw==/7697680907952232.jpg?param=130y130",new SongInfo("Major Lazer","Lean On","Lean OnLean On"));

     */

    private MusicInfo[] musics = {
            new MusicInfo(1313102149,"泡沫","1.jpg"),
            new MusicInfo(2097278594,"顶一万句","2.jpg"),
            new MusicInfo(115162,"月半小夜曲","3.jpg"),
            new MusicInfo(202369,"画","4.jpg"),
            new MusicInfo(32717112,"Lean On","5.jpg"),
        };

    @RequestMapping("/list")
    public List<MusicInfo> getMusicInfo(){
        List<MusicInfo> data = new ArrayList<>();

        for (int i = 0; i <musics.length; i++) {
            data.add(musics[i]);
        }

        for (int i = 0; i <musics.length; i++) {
            data.add(musics[i]);
        }

        return data;
    }

    @RequestMapping("/list/{count}")
    public List<MusicInfo> getMusicInfoSize(@PathVariable("count") int count){
        List<MusicInfo> data = new ArrayList<MusicInfo>();

        Random r = new Random();

        for (int i = 0; i < count; i++) {
            data.add(musics[r.nextInt(musics.length)]);
        }


        return data;
    }

    String[] pics = {
            "1.jpg","2.jpg","3.jpg","4.jpg","5.jpg","6.jpg"
    };

    @RequestMapping("/images/{count}")
    public List<ImageInfo> getImages(@PathVariable("count") int count){
        List<ImageInfo> list = new ArrayList<>();
        Random r = new Random();

        for (int i = 0; i < count; i++) {
            list.add(new ImageInfo(i,pics[r.nextInt(musics.length)]));
        }
        return list;
    }

}
